package com.qf.controller;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.Sys_car;
import com.qf.service.Sys_carService;
import com.qf.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
//Car控制层
@RestController
public class sys_carController {
    @Autowired
    private Sys_carService sys_carService;
//    按照价格排序
    @RequestMapping(value = "/priceList",method = RequestMethod.GET)
    public BaseResult OrderByPrice(@RequestParam("page")Integer page,@RequestParam("limit")Integer size){
        List<Sys_car>sysCars=sys_carService.fingCarByPrice(page, size);
        PageInfo<Sys_car>sysCarPageInfo=new PageInfo<>(sysCars);
        BaseResult baseResult=new BaseResult();
        baseResult.setCode(9760);
        baseResult.setMessage("ok");
        baseResult.setData(sysCarPageInfo.getList());
        baseResult.setCount(sysCarPageInfo.getTotal());
        return baseResult;
    }
    //按照热门排序
    @RequestMapping(value = "/hotList",method = RequestMethod.GET)
    public BaseResult OrderByHot(@RequestParam("page")Integer page,@RequestParam("limit") Integer size){
        List<Sys_car>sysCars=sys_carService.fingCarByNumber(page,size);
        PageInfo<Sys_car>carPageInfo=new PageInfo<>(sysCars);
        BaseResult baseResult=new BaseResult();
        baseResult.setCode(9760);
        baseResult.setMessage("ok");
        baseResult.setData(carPageInfo.getList());
        baseResult.setCount(carPageInfo.getTotal());
        return baseResult;
    }

    //增加汽车
    @RequestMapping(value = "/addCar",method = RequestMethod.POST)
    public BaseResult addCar(Sys_car sysCar){
        BaseResult baseResult = sys_carService.addCar(sysCar);
        return baseResult;
    }
    //判断查询到的用户集合数据
    @RequestMapping(value = "/findCarByName",method = RequestMethod.POST)
    public BaseResult findCarByName(@RequestParam("page")Integer page,@RequestParam("limit")Integer size,@RequestParam("name") String name){
        System.out.println(name);
        List<Sys_car>cars;
        BaseResult baseResult = null;
        if (name==null||"".equals(name)){
            baseResult.setCode(404);
            baseResult.setMessage("用户名不能为空");
            baseResult.setData(null);
            baseResult.setCount(0L);
            return baseResult;
        }else {
            cars=sys_carService.fingCarByName(page, size, name);
        }
        System.out.println(cars);
        if (cars==null||cars.size()==0){
            baseResult.setCode(404);
            baseResult.setMessage("查询结果为空");
            baseResult.setData(null);
            baseResult.setCount(0L);
            return baseResult;
        }else {
            PageInfo<Sys_car>sysCarPageInfo=new PageInfo<>(cars);
            baseResult.setCode(9760);
            baseResult.setMessage(null);
            baseResult.setData(sysCarPageInfo.getList());
            baseResult.setCount(sysCarPageInfo.getTotal());
        }
        return baseResult;
    };
}
